package us.codecraft.tinyioc.learn;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyTest {
    public static void main(String[] args) {
        //获取接口类的增强Class对象
        try {
            Class<?> proxyClass = Proxy.getProxyClass(ProxyTest.class.getClassLoader(), Calc.class);
            Constructor<?> constructor = proxyClass.getConstructor(InvocationHandler.class);
            Calc instance = (Calc)constructor.newInstance(new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    String typeName = proxy.getClass().getTypeName();
                    System.out.println("simpleName = " + typeName);
                    String name = method.getName();
                    System.out.println("name = " + name);
                    return 1;
                }
            });
            System.out.println(instance.add());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
